/**
 * 
 */
package cn.gov.msa.ws;

import java.util.List;

import org.springframework.context.ApplicationContext;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Apply;
import cn.gov.msa.util.GsonHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */

public class ApplyService {
	
	public static String list(ApplicationContext ctx,String params) throws Exception {
	    JSONObject jsonResult = new JSONObject();
	    cn.gov.msa.service.ApplyService applyService = (cn.gov.msa.service.ApplyService) ctx.getBean("ApplyService");  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		int pageNum = 1;
		int pageSize = 5;
		if(jsonParams.has("pageNum"))
		{
			pageNum = jsonParams.getInt("pageNum");
			jsonParams.remove("pageNum");
		}
		if(jsonParams.has("pageSize"))
		{
			pageSize = jsonParams.getInt("pageSize");
			jsonParams.remove("pageSize");
		}	
		Apply apply = GsonHelper.convertEntity(jsonParams.toString(), Apply.class);
		List<Apply> applyList = applyService.selectData(pageNum, pageSize, apply);
		JSONArray jaResult = JSONArray.fromObject(applyList);
	    //获取分页信息
        PageInfo page = new PageInfo(applyList);
        
        jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", jaResult);	
		
		return jsonResult.toString();
	}
	
	
	public static String listHadDone(ApplicationContext ctx,String params) throws Exception {
	    JSONObject jsonResult = new JSONObject();
	    cn.gov.msa.service.ApplyService applyService = (cn.gov.msa.service.ApplyService) ctx.getBean("ApplyService");  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		int pageNum = 1;
		int pageSize = 5;
		if(jsonParams.has("pageNum"))
		{
			pageNum = jsonParams.getInt("pageNum");
			jsonParams.remove("pageNum");
		}
		if(jsonParams.has("pageSize"))
		{
			pageSize = jsonParams.getInt("pageSize");
			jsonParams.remove("pageSize");
		}	
		Apply apply = GsonHelper.convertEntity(jsonParams.toString(), Apply.class);
		List<Apply> applyList = applyService.selectDataHadDone(pageNum, pageSize, apply);
		JSONArray jaResult = JSONArray.fromObject(applyList);
	    //获取分页信息
        PageInfo page = new PageInfo(applyList);
        
        jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", jaResult);	
		
		return jsonResult.toString();
	}
	
}
